<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/27/027
 * Time: 15:08
 */

namespace App\Controller;




use App\Model\EmailModel;
use App\Model\UserModel;
use lib\View;

class EmailController
{

   public function send_email(){
       $p=$_POST;
      // print_r($p['addressee']);exit();
       $p['sendtime']=date('y-m-d h:i:s',time());
       $username=$_SESSION['username'];
       $user=new UserModel('user','uid');
       $userinfo=$user->get(['username'=>$username]);
      if (count($user->get(['email'=>$p['addressee']]))>0){
          // print_r($checkemail);exit();
          if ($userinfo[0]['email']===$p['addressee']){
              echo '<script>alert("您不能发送邮件给自己");history.back();</script>';
          }else {
              $email = new EmailModel('email', 'eid');
              if ($email->save($p)) {
                  echo '<script>alert("发送成功");history.back();</script>';
              }
          }
      }else{
          echo '<script>alert("您发送的邮箱不存在");history.back();</script>';
      }

   }
   public function delete_email(){
       $eid=$_GET;
       $email=new EmailModel('email','eid');
      if ($email->delete($eid)){
          $username=$_SESSION['username'];
          //echo $username;exit();
          $user=new UserModel('user','uid');
          $userinfo=$user->get(['username'=>$username]);
          $emails=$email->get(['addressee'=>$userinfo[0]['email']]);
          return new View('email',['emails'=>$emails]);
      }
   }
    public function email(){
        $userinfo=isset($_SESSION['userinfo'])?$_SESSION['userinfo']:[];
        $useremail=$userinfo[0]['email'];
        // print_r($email);exit();
        $email=new EmailModel('email','eid');
        $emails=$email->get(['addressee'=>$useremail]);
        return new View('email',['emails'=>$emails]);
    }

   public function email_detail(){
       $eid=$_GET['eid'];
       $email=new EmailModel('email','eid');
       $emails=$email->get(['eid'=>$eid]);
      // print_r($emails);exit();
       return new View('email_detail',['emails'=>$emails]);
   }
   public function change_status(){
     $s=$_GET;
     $username=$_SESSION['username'];
     //echo $username;exit();
       $user=new UserModel('user','uid');
       $userinfo=$user->get(['username'=>$username]);
     // print_r($userinfo);exit();
    // print_r($s);exit();

     $email=new EmailModel('email','eid');
     $email->save($s);
     $emails=$email->get(['addressee'=>$userinfo[0]['email']]);
     return new View('email',['emails'=>$emails]);
   }
}